/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.forms.tdkhoa;

import com.aptech.dao.SupplierManager;
import com.aptech.forms.nhanh.SupplierList;
import com.aptech.utility.DBType;
import com.aptech.utility.DBUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author SONY
 */
public class Report extends javax.swing.JFrame {

    private static final String SUPPLIERID_SQL_FILTER
            = "SELECT SupplierTypes.SupplierTypeName, Suppliers.SupplierID,Suppliers.SupplierName,Suppliers.SupplierAddress,Suppliers.SupplierPhone,Suppliers.SupplierEmail"
            + " FROM Suppliers"
            + " INNER JOIN SupplierTypes"
            + " ON Suppliers.SupplierTypeID = SupplierTypes.SupplierTypeID AND Suppliers.SupplierTypeID=";
    private static final String SUPPLIERID_SQL_ALL
            = "SELECT SupplierTypes.SupplierTypeName, Suppliers.SupplierID,Suppliers.SupplierName,Suppliers.SupplierAddress,Suppliers.SupplierPhone,Suppliers.SupplierEmail"
            + " FROM Suppliers"
            + " INNER JOIN SupplierTypes"
            + " ON Suppliers.SupplierTypeID = SupplierTypes.SupplierTypeID";
    private static final String ORDER_FILTER_CUSTOMERNAME
            = "SELECT Customers.CustomerName,Orders.OrderID,Orders.CustomerID,Orders.OrderDate,Orders.ShippedDate,Orders.SubTotal,Orders.Status"
            + " FROM Orders"
            + " INNER JOIN Customers"
            + " ON Orders.CustomerID = Customers.CustomerID AND Customers.CustomerName=";
    private static final String ORDER_FILTER_CUSTOMERID
            = "SELECT Customers.CustomerName,Orders.OrderID,Orders.CustomerID,Orders.OrderDate,Orders.ShippedDate,Orders.SubTotal,Orders.Status"
            + " FROM Orders"
            + " INNER JOIN Customers"
            + " ON Orders.CustomerID = Customers.CustomerID AND Orders.CustomerID=";
    private static final String ORDER_FILTER_STATUS
            = "SELECT Customers.CustomerName,Orders.OrderID,Orders.CustomerID,Orders.OrderDate,Orders.ShippedDate,Orders.SubTotal,Orders.Status"
            + " FROM Orders"
            + " INNER JOIN Customers"
            + " ON Orders.CustomerID = Customers.CustomerID AND Orders.Status=";
    private static final String SALEINVOICE_FILTER_SALEID = "SELECT Customers.CustomerID"
            + ", Customers.CustomerName"
            + ", SalesInvoice.SalesInvoiceID"
            + ", Drugs.DrugName"
            + ", SalesInvoiceDetails.UnitPrice"
            + ", SalesInvoiceDetails.Quantity"
            + ", SalesInvoiceDetails.Discount"
            + ", SalesInvoice.SubTotal"
            + ", SalesInvoice.ModeOfPayment"
            + ", SalesInvoice.Date"
            + " FROM SalesInvoice"
            + " INNER JOIN Customers"
            + " ON SalesInvoice.CustomerID = Customers.CustomerID"
            + " INNER JOIN SalesInvoiceDetails"
            + " ON SalesInvoice.SalesInvoiceID = SalesInvoiceDetails.SalesInvoiceID"
            + " INNER JOIN Drugs"
            + " ON SalesInvoiceDetails.DrugID = Drugs.DrugID AND SalesInvoice.SalesInvoiceID=";
    private static final String SALEINVOICE_FILTER_CUSTOMERID = "SELECT Customers.CustomerID"
            + ", Customers.CustomerName"
            + ", SalesInvoice.SalesInvoiceID"
            + ", Drugs.DrugName"
            + ", SalesInvoiceDetails.UnitPrice"
            + ", SalesInvoiceDetails.Quantity"
            + ", SalesInvoiceDetails.Discount"
            + ", SalesInvoice.SubTotal"
            + ", SalesInvoice.ModeOfPayment"
            + ", SalesInvoice.Date"
            + " FROM SalesInvoice"
            + " INNER JOIN Customers"
            + " ON SalesInvoice.CustomerID = Customers.CustomerID"
            + " INNER JOIN SalesInvoiceDetails"
            + " ON SalesInvoice.SalesInvoiceID = SalesInvoiceDetails.SalesInvoiceID"
            + " INNER JOIN Drugs"
            + " ON SalesInvoiceDetails.DrugID = Drugs.DrugID AND SalesInvoice.CustomerID=";
    private static final String SALEINVOICE_FILTER_CUSTOMERNAME = "SELECT Customers.CustomerID"
            + ", Customers.CustomerName"
            + ", SalesInvoice.SalesInvoiceID"
            + ", Drugs.DrugName"
            + ", SalesInvoiceDetails.UnitPrice"
            + ", SalesInvoiceDetails.Quantity"
            + ", SalesInvoiceDetails.Discount"
            + ", SalesInvoice.SubTotal"
            + ", SalesInvoice.ModeOfPayment"
            + ", SalesInvoice.Date"
            + " FROM SalesInvoice"
            + " INNER JOIN Customers"
            + " ON SalesInvoice.CustomerID = Customers.CustomerID"
            + " INNER JOIN SalesInvoiceDetails"
            + " ON SalesInvoice.SalesInvoiceID = SalesInvoiceDetails.SalesInvoiceID"
            + " INNER JOIN Drugs"
            + " ON SalesInvoiceDetails.DrugID = Drugs.DrugID AND Customers.CustomerName=";
    private static final String SALEINVOICE_FILTER_MODE = "SELECT Customers.CustomerID"
            + ", Customers.CustomerName"
            + ", SalesInvoice.SalesInvoiceID"
            + ", Drugs.DrugName"
            + ", SalesInvoiceDetails.UnitPrice"
            + ", SalesInvoiceDetails.Quantity"
            + ", SalesInvoiceDetails.Discount"
            + ", SalesInvoice.SubTotal"
            + ", SalesInvoice.ModeOfPayment"
            + ", SalesInvoice.Date"
            + " FROM SalesInvoice"
            + " INNER JOIN Customers"
            + " ON SalesInvoice.CustomerID = Customers.CustomerID"
            + " INNER JOIN SalesInvoiceDetails"
            + " ON SalesInvoice.SalesInvoiceID = SalesInvoiceDetails.SalesInvoiceID"
            + " INNER JOIN Drugs"
            + " ON SalesInvoiceDetails.DrugID = Drugs.DrugID AND SalesInvoice.ModeOfPayment=";
    private static final String ORDERDETAILS_FILTER_ID = "SELECT Customers.CustomerName"
            + ",OrderDetails.OrderID"
            + ",OrderDetails.DrugID"
            + ",Drugs.DrugName"
            + ",OrderDetails.UnitPrice"
            + ",OrderDetails.Quantity"
            + ",OrderDetails.Discount"
            + " FROM Orders"
            + " INNER JOIN Customers"
            + " ON Orders.CustomerID = Customers.CustomerID"
            + " INNER JOIN OrderDetails"
            + " ON OrderDetails.OrderID = Orders.OrderID"
            + " INNER JOIN Drugs"
            + " ON OrderDetails.DrugID = Drugs.DrugID AND OrderDetails.OrderID=";

    /**
     * Creates new form Report
     */
    public Report() {
        initComponents();
        setLocationRelativeTo(null);
        loadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog_ReportSupplierType = new javax.swing.JDialog();
        jlblAccList_Username = new javax.swing.JLabel();
        jbtReport_SubpplierGetOne = new javax.swing.JButton();
        jcbReport_SupplierTypeID = new javax.swing.JComboBox();
        jbtReport_SubpplierGetAll = new javax.swing.JButton();
        jDialog_ReportOrderFilter = new javax.swing.JDialog();
        cbFilter = new javax.swing.JComboBox();
        jlblAccList_Username1 = new javax.swing.JLabel();
        jtfReport_FilterOrder = new javax.swing.JTextField();
        jbtExportOrder = new javax.swing.JButton();
        jlblAccList_Username2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jDialog_ReportSaleInvoiceFilter = new javax.swing.JDialog();
        cbSaleInvoiceFilter = new javax.swing.JComboBox();
        jlblAccList_Username3 = new javax.swing.JLabel();
        jtfReport_FilterSaleInvoice = new javax.swing.JTextField();
        jbtExportSaleInvoice = new javax.swing.JButton();
        jlblAccList_Username4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton_ReportCustomer = new javax.swing.JButton();
        jButton_ReportDrug = new javax.swing.JButton();
        jButton_ReportSaleInvoice = new javax.swing.JButton();
        jButton_ReportOrder = new javax.swing.JButton();
        jButton_ReportSupplier = new javax.swing.JButton();

        jDialog_ReportSupplierType.setTitle("Fillter Supplier");

        jlblAccList_Username.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username.setText("Supplier Type ID:");

        jbtReport_SubpplierGetOne.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtReport_SubpplierGetOne.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/003.png"))); // NOI18N
        jbtReport_SubpplierGetOne.setText("Get One");
        jbtReport_SubpplierGetOne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtReport_SubpplierGetOneActionPerformed(evt);
            }
        });

        jcbReport_SupplierTypeID.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jbtReport_SubpplierGetAll.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtReport_SubpplierGetAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/003.png"))); // NOI18N
        jbtReport_SubpplierGetAll.setText("Get All");
        jbtReport_SubpplierGetAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtReport_SubpplierGetAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialog_ReportSupplierTypeLayout = new javax.swing.GroupLayout(jDialog_ReportSupplierType.getContentPane());
        jDialog_ReportSupplierType.getContentPane().setLayout(jDialog_ReportSupplierTypeLayout);
        jDialog_ReportSupplierTypeLayout.setHorizontalGroup(
            jDialog_ReportSupplierTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog_ReportSupplierTypeLayout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jDialog_ReportSupplierTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jDialog_ReportSupplierTypeLayout.createSequentialGroup()
                        .addComponent(jbtReport_SubpplierGetAll)
                        .addGap(73, 73, 73)
                        .addComponent(jbtReport_SubpplierGetOne)
                        .addGap(13, 13, 13))
                    .addGroup(jDialog_ReportSupplierTypeLayout.createSequentialGroup()
                        .addComponent(jlblAccList_Username)
                        .addGap(31, 31, 31)
                        .addComponent(jcbReport_SupplierTypeID, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jDialog_ReportSupplierTypeLayout.setVerticalGroup(
            jDialog_ReportSupplierTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog_ReportSupplierTypeLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(jDialog_ReportSupplierTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblAccList_Username)
                    .addComponent(jcbReport_SupplierTypeID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jDialog_ReportSupplierTypeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtReport_SubpplierGetOne, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtReport_SubpplierGetAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(22, 22, 22))
        );

        jDialog_ReportOrderFilter.setTitle("Order Report Filter");

        cbFilter.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        cbFilter.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Customer Name", "Customer ID", "Status", "Order ID" }));

        jlblAccList_Username1.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username1.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username1.setText("Filter by");

        jtfReport_FilterOrder.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jbtExportOrder.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtExportOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/026.png"))); // NOI18N
        jbtExportOrder.setText("Export");
        jbtExportOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtExportOrderActionPerformed(evt);
            }
        });

        jlblAccList_Username2.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username2.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username2.setText("Filter value");

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/009.png"))); // NOI18N
        jLabel1.setText("Order Report Filter");

        javax.swing.GroupLayout jDialog_ReportOrderFilterLayout = new javax.swing.GroupLayout(jDialog_ReportOrderFilter.getContentPane());
        jDialog_ReportOrderFilter.getContentPane().setLayout(jDialog_ReportOrderFilterLayout);
        jDialog_ReportOrderFilterLayout.setHorizontalGroup(
            jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog_ReportOrderFilterLayout.createSequentialGroup()
                .addGroup(jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog_ReportOrderFilterLayout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlblAccList_Username1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblAccList_Username2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfReport_FilterOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jbtExportOrder)
                                .addComponent(cbFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jDialog_ReportOrderFilterLayout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(jLabel1)))
                .addContainerGap(87, Short.MAX_VALUE))
        );
        jDialog_ReportOrderFilterLayout.setVerticalGroup(
            jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog_ReportOrderFilterLayout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfReport_FilterOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Username2))
                .addGap(18, 18, 18)
                .addGroup(jDialog_ReportOrderFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Username1))
                .addGap(18, 18, 18)
                .addComponent(jbtExportOrder)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jDialog_ReportSaleInvoiceFilter.setTitle("Sale Invoice Report Filter");

        cbSaleInvoiceFilter.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        cbSaleInvoiceFilter.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sale Invoice ID", "Customer Name", "Customer ID", "Mode of Payment" }));

        jlblAccList_Username3.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username3.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username3.setText("Filter by");

        jtfReport_FilterSaleInvoice.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N

        jbtExportSaleInvoice.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jbtExportSaleInvoice.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/026.png"))); // NOI18N
        jbtExportSaleInvoice.setText("Export");
        jbtExportSaleInvoice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtExportSaleInvoiceActionPerformed(evt);
            }
        });

        jlblAccList_Username4.setFont(new java.awt.Font("Segoe UI Light", 1, 14)); // NOI18N
        jlblAccList_Username4.setForeground(new java.awt.Color(51, 0, 255));
        jlblAccList_Username4.setText("Filter value");

        jLabel2.setFont(new java.awt.Font("Segoe UI Light", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/009.png"))); // NOI18N
        jLabel2.setText("Sale Invoice Report Filter");

        javax.swing.GroupLayout jDialog_ReportSaleInvoiceFilterLayout = new javax.swing.GroupLayout(jDialog_ReportSaleInvoiceFilter.getContentPane());
        jDialog_ReportSaleInvoiceFilter.getContentPane().setLayout(jDialog_ReportSaleInvoiceFilterLayout);
        jDialog_ReportSaleInvoiceFilterLayout.setHorizontalGroup(
            jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createSequentialGroup()
                .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlblAccList_Username3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jlblAccList_Username4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfReport_FilterSaleInvoice, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jbtExportSaleInvoice)
                                .addComponent(cbSaleInvoiceFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(jLabel2)))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jDialog_ReportSaleInvoiceFilterLayout.setVerticalGroup(
            jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(7, 7, 7)
                .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfReport_FilterSaleInvoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Username4))
                .addGap(18, 18, 18)
                .addGroup(jDialog_ReportSaleInvoiceFilterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbSaleInvoiceFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblAccList_Username3))
                .addGap(18, 18, 18)
                .addComponent(jbtExportSaleInvoice)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Report");
        setIconImage(new ImageIcon(getClass().getResource("/com/image/icon.png")).getImage());

        jPanel1.setPreferredSize(new java.awt.Dimension(300, 300));
        jPanel1.setLayout(new java.awt.GridLayout(3, 3, 20, 20));

        jButton_ReportCustomer.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jButton_ReportCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/035.png"))); // NOI18N
        jButton_ReportCustomer.setText("<html>Customer<br />Report</html>");
        jButton_ReportCustomer.setToolTipText("");
        jButton_ReportCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReportCustomerActionPerformed(evt);
            }
        });
        jPanel1.add(jButton_ReportCustomer);

        jButton_ReportDrug.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jButton_ReportDrug.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/036.png"))); // NOI18N
        jButton_ReportDrug.setText("<html>Drug<br />Report</html>");
        jButton_ReportDrug.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReportDrugActionPerformed(evt);
            }
        });
        jPanel1.add(jButton_ReportDrug);

        jButton_ReportSaleInvoice.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jButton_ReportSaleInvoice.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/039.png"))); // NOI18N
        jButton_ReportSaleInvoice.setText("<html>Sale<br />Invoice</html>");
        jButton_ReportSaleInvoice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReportSaleInvoiceActionPerformed(evt);
            }
        });
        jPanel1.add(jButton_ReportSaleInvoice);

        jButton_ReportOrder.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jButton_ReportOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/038.png"))); // NOI18N
        jButton_ReportOrder.setText("<html>Order<br />Report</html>");
        jButton_ReportOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReportOrderActionPerformed(evt);
            }
        });
        jPanel1.add(jButton_ReportOrder);

        jButton_ReportSupplier.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N
        jButton_ReportSupplier.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/image/037.png"))); // NOI18N
        jButton_ReportSupplier.setText("<html>Supplier<br />Report</html>");
        jButton_ReportSupplier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ReportSupplierActionPerformed(evt);
            }
        });
        jPanel1.add(jButton_ReportSupplier);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_ReportCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReportCustomerActionPerformed
        InputStream is = getClass().getResourceAsStream("/report/customerReport.jrxml");
        if (is != null) {
            exportReport(is);
        } else {
            JOptionPane.showMessageDialog(null, "Cannot find report!\nPlease, contact your administrator", "Message", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jButton_ReportCustomerActionPerformed

    private void jButton_ReportDrugActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReportDrugActionPerformed
        // Export Drug report
        InputStream is = getClass().getResourceAsStream("/report/drugReport.jrxml");
        if (is != null) {
            exportReport(is);
        } else {
            JOptionPane.showMessageDialog(null, "Cannot find report!\nPlease, contact your administrator", "Message", JOptionPane.ERROR_MESSAGE);

        }
    }//GEN-LAST:event_jButton_ReportDrugActionPerformed

    private void jButton_ReportSaleInvoiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReportSaleInvoiceActionPerformed
        jDialog_ReportSaleInvoiceFilter.setVisible(true);
        jDialog_ReportSaleInvoiceFilter.pack();
        jDialog_ReportSaleInvoiceFilter.setLocationRelativeTo(null);
    }//GEN-LAST:event_jButton_ReportSaleInvoiceActionPerformed

    private void jButton_ReportOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReportOrderActionPerformed
        jDialog_ReportOrderFilter.setVisible(true);
        jDialog_ReportOrderFilter.pack();
        jDialog_ReportOrderFilter.setLocationRelativeTo(null);
    }//GEN-LAST:event_jButton_ReportOrderActionPerformed

    private void jButton_ReportSupplierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ReportSupplierActionPerformed

        jDialog_ReportSupplierType.setVisible(true);
        jDialog_ReportSupplierType.pack();
        jDialog_ReportSupplierType.setLocationRelativeTo(null);
    }//GEN-LAST:event_jButton_ReportSupplierActionPerformed

    private void jbtReport_SubpplierGetOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtReport_SubpplierGetOneActionPerformed
        InputStream is = getClass().getResourceAsStream("/report/supplierReport.jrxml");
        ComboItem item = (ComboItem) jcbReport_SupplierTypeID.getSelectedItem();

        exportReport(is, SUPPLIERID_SQL_FILTER, item.value.trim());
    }//GEN-LAST:event_jbtReport_SubpplierGetOneActionPerformed

    private void jbtReport_SubpplierGetAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtReport_SubpplierGetAllActionPerformed
        InputStream is = getClass().getResourceAsStream("/report/supplierReport.jrxml");
        exportReport(is, SUPPLIERID_SQL_ALL);
    }//GEN-LAST:event_jbtReport_SubpplierGetAllActionPerformed

    private void jbtExportOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtExportOrderActionPerformed
        InputStream is = getClass().getResourceAsStream("/report/orderReport.jrxml");
        InputStream isNew = getClass().getResourceAsStream("/report/orderDetailsReport.jrxml");
        int index = cbFilter.getSelectedIndex();
        switch (index) {
            case 0:
                exportReport(is, ORDER_FILTER_CUSTOMERNAME, jtfReport_FilterOrder.getText().trim());
                break;
            case 1:
                exportReport(is, ORDER_FILTER_CUSTOMERID, jtfReport_FilterOrder.getText().trim());
                break;
            case 2:
                exportReport(is, ORDER_FILTER_STATUS, jtfReport_FilterOrder.getText().trim().toUpperCase());
                break;
            case 3:
                exportReport(isNew, ORDERDETAILS_FILTER_ID, jtfReport_FilterOrder.getText().trim());
                break;
        }
    }//GEN-LAST:event_jbtExportOrderActionPerformed

    private void jbtExportSaleInvoiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtExportSaleInvoiceActionPerformed
        InputStream is = getClass().getResourceAsStream("/report/saleInvoiceReport.jrxml");
        int index = cbSaleInvoiceFilter.getSelectedIndex();
        switch (index) {
            case 0:
                exportReport(is, SALEINVOICE_FILTER_SALEID, jtfReport_FilterSaleInvoice.getText().trim());
                break;
            case 1:
                exportReport(is, SALEINVOICE_FILTER_CUSTOMERNAME, jtfReport_FilterSaleInvoice.getText().trim());
                break;
            case 2:
                exportReport(is, SALEINVOICE_FILTER_CUSTOMERID, jtfReport_FilterSaleInvoice.getText().trim().toUpperCase());
                break;
            case 3:
                exportReport(is, SALEINVOICE_FILTER_MODE, jtfReport_FilterSaleInvoice.getText().trim().toUpperCase());
                break;
        }
    }//GEN-LAST:event_jbtExportSaleInvoiceActionPerformed

    public static void exportReport(InputStream is) {
        try {
            JasperDesign jasperDesign = JRXmlLoader.load(is);
            JasperReport jr = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, DBUtil.getConnection(DBType.myConnection));
            JasperViewer.viewReport(jp, false);
        } catch (JRException ex) {
            JOptionPane.showMessageDialog(null, "Cannot load report", "Connection error", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Connection failed\nTry again", "Connection error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void exportReport(InputStream is, String query) {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            JasperDesign jd = JRXmlLoader.load(is);
            JRDesignQuery newQuery = new JRDesignQuery();
            newQuery.setText(query);
            jd.setQuery(newQuery);
            JasperReport jr = JasperCompileManager.compileReport(jd);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, DBUtil.getConnection(DBType.myConnection));
            JasperViewer.viewReport(jp, false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Connection failed\nTry again", "Connection error", JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JRException ex) {
           JOptionPane.showMessageDialog(null, "Cannot load report", "Connection error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void exportReport(InputStream is, String query, String parameter) {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            JasperDesign jd = JRXmlLoader.load(is);
            JRDesignQuery newQuery = new JRDesignQuery();
            newQuery.setText(query + "'" + parameter + "'");
            jd.setQuery(newQuery);
            JasperReport jr = JasperCompileManager.compileReport(jd);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, DBUtil.getConnection(DBType.myConnection));
            JasperViewer.viewReport(jp, false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Connection failed\nTry again", "Connection error", JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JRException ex) {
            JOptionPane.showMessageDialog(null, "Cannot load report", "Connection error", JOptionPane.ERROR_MESSAGE);
        }

    }

    /* Load data to table */
    private void loadData() {

        try {
            // Fill data into combobox cbTypeID
            if (jcbReport_SupplierTypeID.getItemCount() == 0) {
                ResultSet rsTypeID = SupplierManager.getTypeID();
                while (rsTypeID.next()) {
                    jcbReport_SupplierTypeID.addItem(new ComboItem(rsTypeID.getString(2), String.valueOf(rsTypeID.getInt(1)).trim()));
                }
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Connection failed\nTry again", "Connection error", JOptionPane.ERROR_MESSAGE);
        }
    }

    class ComboItem {

        private String key;
        private String value;

        public ComboItem(String key, String value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String toString() {
            return key;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Report.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Report.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Report.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Report.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Report().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbFilter;
    private javax.swing.JComboBox cbSaleInvoiceFilter;
    private javax.swing.JButton jButton_ReportCustomer;
    private javax.swing.JButton jButton_ReportDrug;
    private javax.swing.JButton jButton_ReportOrder;
    private javax.swing.JButton jButton_ReportSaleInvoice;
    private javax.swing.JButton jButton_ReportSupplier;
    private javax.swing.JDialog jDialog_ReportOrderFilter;
    private javax.swing.JDialog jDialog_ReportSaleInvoiceFilter;
    private javax.swing.JDialog jDialog_ReportSupplierType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtExportOrder;
    private javax.swing.JButton jbtExportSaleInvoice;
    private javax.swing.JButton jbtReport_SubpplierGetAll;
    private javax.swing.JButton jbtReport_SubpplierGetOne;
    private javax.swing.JComboBox jcbReport_SupplierTypeID;
    private javax.swing.JLabel jlblAccList_Username;
    private javax.swing.JLabel jlblAccList_Username1;
    private javax.swing.JLabel jlblAccList_Username2;
    private javax.swing.JLabel jlblAccList_Username3;
    private javax.swing.JLabel jlblAccList_Username4;
    private javax.swing.JTextField jtfReport_FilterOrder;
    private javax.swing.JTextField jtfReport_FilterSaleInvoice;
    // End of variables declaration//GEN-END:variables
}
